<?php
class Client{
	private $id;
	private $firstName;
	private $lastName;
	private $user;
	private $password;
	private	$birthdate;
	private $address;
	private $zipCode;
	private $city;
	private $country;
	private $cardtype;
	private $cardNumber;

	/*
	 * Constructor
	 */
	function __construct($id){
		$this->id=$id;
		$this->recoveryfromfile($this->id);
	}
	
	private function recoveryfromfile($id){
		$serverpath=$_SERVER['DOCUMENT_ROOT'];
		$filename=$serverpath.'/files/client/'.$id.'.txt';
		$file = fopen($filename,"r");
		$line = fgets($file);
		$info = explode('|',$line);
		$this->set_firstname($info[0]);
		$this->set_lastname($info[1]);
		$this->set_user($info[2]);
		$this->set_password($info[3]);
		$this->set_birthdate($info[4]);
		$this->set_country($info[5]);
		$this->set_address($info[6]);
		$this->set_zipCode($info[7]);
		$this->set_city($info[8]);
		$this->set_cardtype($info[9]);
		$this->set_cardNumber($info[10]);
		fclose($file);
	}
	public function savetofile($id){
		$serverpath=$_SERVER['DOCUMENT_ROOT'];
		$filename=$serverpath.'/files/client/'.$id.'.txt';
		$file = fopen($filename,"w");
		$Stringline = $this->firstName.'|'.$this->lastName.'|'.$this->get_user().'|'.$this->password.'|'.
		$this->birthdate.'|'.$this->country.'|'.$this->address.'|'.$this->zipCode.'|'.$this->city.'|'.
		$this->cardtype.'|'.$this->cardNumber;
		fwrite($file,$Stringline);
		fclose($file);
	}
	public function savepassword($id){
		$serverpath=$_SERVER['DOCUMENT_ROOT'];
		$filename=$serverpath.'/users.txt';
		$filename_tmp = $serverpath.'/users_tmp.txt';
		$file = fopen($filename,"r");
		$file_tmp = fopen ($filename_tmp,"w");
		if ($file){
		while (!feof($file)) {
			$line=fgets($file);
			$info = explode('|',$line);
			if ($info[0]==$id){
				//reemplazo la linea en el temporal
				$newLine = $id.'|'.$this->get_user().'|'.$this->get_password();
				fwrite($file_tmp,$newLine);
			}else{
				fwrite($file_tmp,$line);
			}	
		}
		fclose($file);
		fclose($file_tmp);
	}
		
	}
	
	public function get_id(){
		return $this->id;
	}
	
	public function get_user(){
		return $this->user;
	}
	private function set_user($user){
		$this->user=$user;
	}
	
	public function get_firstname(){
		return $this->firstName;
	}
	public function set_firstname($firstName){
		$this->firstName=$firstName;
	}
	
	public function get_lastname(){
		return $this->lastName;
	}
	public function set_lastname($lastName){
		$this->lastName=$lastName;
	}
	
	public function get_password(){
		return $this->password;
	}
	public function set_password($password){
		$this->password=$password;
	}
	
	public function get_birthdate(){
		return $this->birthdate;
	}
	public function set_birthdate($birthdate){
		$this->birthdate=$birthdate;
	}

	public function get_country(){
		return $this->country;
	}
	public function set_country($country){
		$this->country=$country;
	}
	
	public function get_address(){
		return $this->address;
	}
	public function set_address($address){
		$this->address=$address;
	}
	
	public function get_zipCode(){
		return $this->zipCode;
	}
	public function set_zipCode($zipCode){
		$this->zipCode=$zipCode;
	}
	
	public function get_city(){
		return $this->city;
	}
	public function set_city($city){
		$this->city=$city;
	}
	
	public function get_cardtype(){
		return $this->cardtype;
	}
	public function set_cardtype($cardtype){
		$this->cardtype=$cardtype;
	}
	
	public function get_cardNumber(){
		return $this->cardNumber;
	}
	public function set_cardNumber($cardNumber){
		$this->cardNumber=$cardNumber;
	}
}